﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PMQLCHH.DAO;



namespace PMQLCHH.BUS
{
    class HoaDonBUS
    {
        //Kha Nguyen - Hoa Don BUS - 6/4/2013 - begin 
        /// <summary>
        /// Lap Hoa Don
        /// </summary>
        /// 
        public string LapHoaDon(HoaDonDTO GioHang)
        {
            HoaDonDAO HDDAO = new HoaDonDAO();
            DataTable dt = HDDAO.LayMaHoaDonMoiNhat();
            string MaHD = "";
            if (dt.Rows.Count > 0)
            {
                MaHD = "HD" + (int.Parse(dt.Rows[0]["MaHoaDon"].ToString().Substring(2)) + 1).ToString();
            }
            else
            {
                MaHD = "HD1000001";
            }
            GioHang.StrMaHoaDon = MaHD;
            bool KQ = HDDAO.ThemHoaDon(GioHang);
            if (!KQ)
            {
                MaHD = "0";
            }
            return MaHD;
        }

        /// <summary>
        /// Them chi Tiet Hoa Don
        /// </summary>
        /// 
        public bool ThemChiTietHoaDon(ChiTietHoaDonDTO CTHD)
        {
            bool KQ = false;
            HoaDonDAO HDDAO = new HoaDonDAO();
            //DataTable dt = HDDAO.LayMaChiTietHoaDonMoiNhat();
            //int MaCTHD = 0;
            //if (dt.Rows.Count > 0)
            //{
            //    MaCTHD = int.Parse(dt.Rows[0]["MaChiTietHoaDon"].ToString()) + 1;
            //}
            //else
            //{
            //    MaCTHD = 1;
            //}
            //CTHD.IMaChiTietHoaDon = MaCTHD;
            KQ = HDDAO.ThemChiTietHoaDon(CTHD);
            return KQ;
        }

        /// <summary>
        /// Them chi Tiet Hoa Don
        /// </summary>
        /// 
        public string LayMaNgayLapHoaDonCuNhat()
        {
            HoaDonDAO HDDAO = new HoaDonDAO();
            DataTable dt = HDDAO.LayMaNgayLapHoaDonCuNhat();
            string NgayLap = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            DateTime date = new DateTime();
            if (dt.Rows.Count > 0)
            {
                date = DateTime.Parse(dt.Rows[0]["NgayLap"].ToString());
                NgayLap = date.Month + "/" + date.Day + "/" + date.Year;
            }
            return NgayLap;
        }

        /// <summary>
        /// Lay Danh Sach Hoa Don
        /// </summary>
        /// 
        public DataTable LayDanhSachHoaDon()
        {
            DataTable dt = new DataTable();
            HoaDonDAO HDDao = new HoaDonDAO();
            dt = HDDao.LayDanhSachHoaDon();
            return dt;
        }





        /// <summary>
        /// Cap Nhat Hoa Don
        /// </summary>
        ///
        public bool CapNhatHoaDon(HoaDonDTO HoaDon)
        {
            bool KQ = false;
            HoaDonDAO HDDao = new HoaDonDAO();
            KQ = HDDao.CapNhatHoaDon(HoaDon);
            return KQ;
        }


        /// <summary>
        /// Xoa Hoa Don
        /// </summary>
        ///
        public bool XoaHoaHoaDon(string MaHD)
        {
            bool KQ = false;
            HoaDonDAO HDDao = new HoaDonDAO();
            DataTable dt =  HDDao.LayChiTietHoaDon(MaHD);
            if (dt.Rows.Count != 0)
            {
                if (int.Parse(dt.Rows[0]["TrangThai"].ToString()) == 1)
                {
                    return false;
                }
            }
            KQ = HDDao.XoaChiTietHoaDonTuHoaDon(MaHD);
            KQ = HDDao.XoaHoaDon(MaHD);
            return KQ;
        }

        /// <summary>
        /// Xoa chi tiet Hoa Don
        /// </summary>
        ///
        public bool XoaCTHoaDon(string MaHD)
        {
            bool KQ = false;
            HoaDonDAO HDDao = new HoaDonDAO();
            KQ = HDDao.XoaChiTietHoaDonTuHoaDon(MaHD);        
            return KQ;
        }
        /// <summary>
        /// loc danh sach  hoa don theo cac tieu chi Ma Hoa Don va trang thai, tu ngay den ngay
        /// </summary>
        /// 
        public DataTable TimKiemHoaDon(string MaHD, string strTuNgay, string strDenNgay, int TrangThai)
        {
            DataTable KQ = new DataTable();
            HoaDonDAO HDDao = new HoaDonDAO();
            KQ = HDDao.LocHoaDon(MaHD, strTuNgay, strDenNgay, TrangThai);
            return KQ;
        }


        /// <summary>
        /// lay chi tiet hoa don theo hoa don
        /// </summary>
        /// 
        public HoaDonDTO LayHoaDon(string MaHD)
        {
            HoaDonDAO HDDao = new HoaDonDAO();
            DataTable dt = HDDao.LayChiTietHoaDon(MaHD);
            HoaDonDTO HD = new HoaDonDTO();
            if (dt.Rows.Count > 0)
            {
                HD.StrMaHoaDon = dt.Rows[0]["MaHoaDon"].ToString();
                HD.StrMaKhachHang = dt.Rows[0]["MaKhachHang"].ToString();
                HD.StrSoDienThoaiNguoiNhan = dt.Rows[0]["SoDienThoaiNguoiNhan"].ToString();
                HD.StrTenNguoiNhan = dt.Rows[0]["TenNguoiNhan"].ToString();
                HD.StrGhiChu = dt.Rows[0]["GhiChu"].ToString();
                HD.StrDiaChiGiao = dt.Rows[0]["DiaChiGiao"].ToString();
                //HD.IGiaoHang = int.Parse(dt.Rows[0]["GiaoHang"].ToString());
                HD.ITrangThai = int.Parse(dt.Rows[0]["TrangThai"].ToString());
                HD.IVat = int.Parse(dt.Rows[0]["Vat"].ToString());
                HD.DtNgayGiao = dt.Rows[0]["NgayGiao"].ToString();
                HD.DtNgayLap = dt.Rows[0]["NgayLap"].ToString();
                HD.FConNo = long.Parse(dt.Rows[0]["CongNo"].ToString());
                HD.FPhiVanChuyen = long.Parse(dt.Rows[0]["PhiVanChuyen"].ToString());
                HD.FTongTien = long.Parse(dt.Rows[0]["TongTien"].ToString());
                HD.FTraTruoc = long.Parse(dt.Rows[0]["TraTruoc"].ToString());
            }
            return HD;
        }

        /// <summary>
        /// lay danh sach mat hang da mua trong hoa don
        /// </summary>
        /// 
        public DataTable LayDSMHChiTietHoaDon(string MaHD)
        {
            HoaDonDAO HDDao = new HoaDonDAO();
            DataTable dt = HDDao.LayChiTietHoaDon(MaHD);
            dt = HDDao.LayDSMHChiTietHoaDon(MaHD);
            return dt;
        }


        /// <summary>
        /// cap nhat lai so luong ton cua mat hang sau khi thanh toan
        /// </summary>
        /// 
        public void CapNhatMatHangSauKhiThanhToan(string MaHD)
        {
            HoaDonDAO HDDao = new HoaDonDAO();
            MatHangBUS MHBus = new MatHangBUS();
            DataTable dt = HDDao.LayDSMHChiTietHoaDon(MaHD);
            foreach (DataRow dr in dt.Rows)
            {
                string MaMH = dr["MaMatHang"].ToString();
                DataTable dtt = MHBus.LayChiTietMatHang(MaMH);
                int SoLuongTon = 0;
                if (dtt.Rows.Count != 0)
                {
                    SoLuongTon = int.Parse(dtt.Rows[0]["SoLuongTon"].ToString());
                    SoLuongTon = SoLuongTon - int.Parse(dr["SoLuong"].ToString());
                }
                MHBus.CapNhatSoLuongTon(MaMH, SoLuongTon);
            }
        }
        /// <summary>
        /// lay ngay giao hang
        /// </summary>
        /// 
        public DateTime LayNgayGiaoHang(string MaHD)
        {
            HoaDonDAO HDDao = new HoaDonDAO();
            DataTable dt = HDDao.LayChiTietHoaDon(MaHD);
            return DateTime.Parse(dt.Rows[0]["NgayGiao"].ToString());
        }


        //Kha Nguyen - Hoa Don BUS - 6/4/2013 - end




        #region Quang Dat them bao cao thong ke 11/06/2013

        public static DataTable LayDanhSachHoaDonTheoNgay(string tungay, string denngay)
        {
            DataTable dt = HoaDonDAO.LayDanhSachHoaDonTheoNgay(tungay, denngay);
            return dt;
        }

        public static DataTable Load_List_HoaDon_Theo_Ngay(string ngay)
        {
            DataTable dt = HoaDonDAO.Load_List_HoaDon_Theo_Ngay(ngay);
            return dt;
        }

        #endregion

    }
}
